﻿#ifndef BUSINESSSQL_H
#define BUSINESSSQL_H

#include <sqlite3.h>
#include <nlohmann/json.hpp>
#include "businessuser.h"

#include <soci/soci.h>
#include <soci/mysql/soci-mysql.h>

class BusinessSQL
{
public:
    // 删除拷贝构造函数和拷贝赋值操作符
    BusinessSQL(BusinessSQL const&) = delete;
    BusinessSQL& operator=(BusinessSQL const&) = delete;
    // 提供一个全局唯一的接口
    static BusinessSQL& getInstance() {
        static BusinessSQL instance;
        return instance;
    }

    // init
    void init();
    // connect to a database
    void useDatabase();
    // create table
    void createTable();

    // record message

    void saveUserMessage(uint64_t senderID, uint64_t receiverID, nlohmann::json message);

    nlohmann::json queryLatest20UserMessage(uint64_t senderID, uint64_t receiverID);

    void saveGroupMessage(uint64_t senderID, uint64_t groupID, nlohmann::json message);

    nlohmann::json queryLatest30GroupMessage(uint64_t groupID);

private:

    sqlite3 * db;

    const int POOL_SIZE = 10;
    std::shared_ptr<soci::connection_pool> dbPool;

    BusinessSQL();
    ~BusinessSQL(){    sqlite3_close(db);}
};

#endif // BUSINESSSQL_H
